<?php

namespace mpend\modules\user\models;

use Yii;
use common\models\shop\ShopFavorite;
use common\models\user\UserFavorite;

/**
 * 移除用户收藏的商品、动态内容
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class FavoriteRemoveForm extends \common\base\Model {

    use \common\traits\Constants;

    const TYPE_PRODUCT = 'product';
    const TYPE_ROOM = 'room';
    const TYPE_STORE = 'store';

    /**
     * @var integer 用户的UID
     */
    public $user_id;

    /**
     * @var integer 当前用户ID
     */
    public $ids;

    /**
     * @var string 操作内容类型
     */
    public $type;

    /**
     * @var array 操作内容ID数组
     */
    private $_pks = [];

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['ids', 'type'], 'required'],
            [['user_id'], 'integer'],
            ['type', 'in', 'range' => static::getConstants('type'), 'message' => '{attribute}不正确'],
            [['type'], 'validateType'],
            [['ids'], 'validateIds'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'ids' => '移除内容',
            'user_id' => '用户信息',
            'type' => '类型',
        ];
    }

    public function validateType($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $ids = array_unique(array_filter(explode(',', $this->$attribute)));
        if (empty($ids)) {
            $this->addError($attribute, '操作内容不能为空');
        } else {
            $this->_pks = $ids;
        }
    }

    public function validateIds($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $ids = array_unique(array_filter(explode(',', $this->$attribute)));
        if (empty($ids)) {
            $this->addError($attribute, '操作内容不能为空');
        } else {
            $this->_pks = $ids;
        }
    }

    public function getModels() {
        $ids = $this->_pks;
        $models = [];
        if ($this->type == self::TYPE_PRODUCT) {
            $models = ShopFavorite::findAll([
                'user_id' => $this->user_id,
                'product_id' => $ids,
            ]);
        } elseif ($this->type == self::TYPE_STORE) {
            $models = ShopFavorite::findAll([
                'user_id' => $this->user_id,
                'store_id' => $ids,
            ]);
        } elseif ($this->type == self::TYPE_ROOM) {
            $models = UserFavorite::findAll([
                'user_id' => $this->user_id,
                'model_id' => $ids,
                'model' => $this->type,
            ]);
        }
        return $models;
    }

    public function getModelAttributeName() {
        if ($this->type == static::TYPE_PRODUCT) {
            return $this->type . '_id';
        } elseif ($this->type == static::TYPE_STORE) {
            return $this->type . '_id';
        } elseif ($this->type == static::TYPE_ARTICLE || $this->type == static::TYPE_DYNAMIC) {
            return 'model_id';
        }
        return '';
    }

    public function save() {
        if (!$this->validate()) {
            return $this->dumpError('收藏批量移除失败');
        }
        $datas = $this->getModels();
        if (empty($datas)) {
            return $this->dumpError('收藏批量移除失败');
        }
        $attribute = $this->getModelAttributeName();
        if (!$attribute) {
            return $this->dumpError('不支持的收藏类型');
        }
        $removed = [];
        foreach ($datas as $model) {
            $id = $model->$attribute;
            if ($model->delete()) {
                $removed[] = $id;
            }
        }
        return $removed;
    }

}
